
module.exports = {
    // 获取分类
    get_category: async (ctx,next)=>{
        const {startPage,endPage} = await ctx.service.utils.paging(ctx)
        await ctx.db.query('select * from category where isdel=0 ').then(async res=>{
            const total =  res[0].length
            await ctx.db.query('select * from category where isdel=0  order by createTime desc limit ?,?',[startPage,endPage]).then(ress=>{
                ctx.body = {
                    code:200,
                    msg:'获取成功',
                    category:ress[0],
                    total:total
                }
            })
        })
        
    },
    // 添加分类
    add_category: async (ctx,next)=>{
        const {categoryName, categoryAlias} = ctx.request.body
        const create_time = new Date()
        const isExit =  await ctx.db.query('select categoryName from category where categoryName=?',[categoryName])
        if(isExit[0].length >= 1){
            ctx.body = {
                code:202,
                msg:'该分类已存在'
            }
        }else{
            await ctx.db.query('insert into category(categoryName,categoryAlias,createTime) values(?,?,?)',
            [categoryName, categoryAlias, create_time]
            ).then(res=>{
                if(res[0].affectedRows == 1){
                    ctx.body = {
                        code:200,
                        msg:'创建成功'
                    }
                }
            }).catch(err=>{
                ctx.body = {
                    code:201,
                    msg:'创建失败'+err
                }
            })
        }
    },
    // 修改分类
    update_category: async (ctx, next)=> {
        const { id,categoryName, categoryAlias} = ctx.request.body
        try {
           let [res] = await ctx.db.query('update category set categoryName=?,categoryAlias=? ,updateTime=? where id=?',[categoryName,categoryAlias,new Date(),id])
           if(res.affectedRows >= 1){
                ctx.body = {
                    code:200,
                    msg:'修改成功'
                }
            }
        } catch (error) {
            ctx.body = {
                code:300,
                msg:'修改失败'+error
            }
        }
           
         
    },
    // 删除和恢复分类
    del_category:async (ctx, next)=> {
        const {id} = ctx.request.body
       let [res] = await ctx.db.query('update category set isdel=? where id=?',[1, id])
            if(res.affectedRows == 1){ 
                    ctx.body = {
                        code:200,
                        msg:'删除成功'
                }
            }else{
                ctx.body = {
                    code:300,
                    msg:'删除失败'
                }
            }
        
    },
    detail_category: async (ctx,next)=>{
        const {id} =ctx.request.query 
         
      try {
        let [res] =  await ctx.db.query('select * from category where id=? ',[id])
        return ctx.body= {
            code:200,
            msg:'获取详情成功',
            data:res[0]

        }
      } catch (error) {
       return ctx.body = {
        code:300,
        msg:'获取详情失败'
       } 
      }  
        
    },
}